package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.config.UserInfo;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope  //配套@Value使用
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    int port;
    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
   /* @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {
        User user = userService.queryById(id);
        user.setAddress(port + "");
        return user;
    }*/
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,
                          @RequestHeader(value = "Truth",required = false) String name) {
        System.out.println("请求头中携带的name = " + name);
        return userService.queryById(id);
    }

//    @Value("${pattern.dateformat}")
//    private String dateformat;

    @Autowired
    private PatternProperties patternProperties;

    @GetMapping("/now")
    public String now() {
        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
        SimpleDateFormat sdf = new SimpleDateFormat(patternProperties.getDateformat());
        String time = sdf.format(date);
        return time;
    }

    @GetMapping("/prop")
    public PatternProperties prop() {
        return patternProperties;
    }

    @Autowired
    private UserInfo userInfo;
    @GetMapping("userInfo")
    public UserInfo userInfo(){
        return userInfo;
    }


}
